//
//  UIFont+Extension.m
//  TableDemo
//
//  Created by jutuo on 2020/3/25.
//  Copyright © 2020 jutuo. All rights reserved.
//

#import "UIFont+Extension.h"


@implementation UIFont (Extension)
- (BOOL)isBold
{
    if (![self respondsToSelector:@selector(fontDescriptor)]) {
        return NO;
    }
    return (self.fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold) > 0;
}
- (BOOL)isItalic
{
    if (![self respondsToSelector:@selector(fontDescriptor)]) {
           return NO;
    }
    return (self.fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic) > 0;
}

- (BOOL)isMonoSpace
{
    if (![self respondsToSelector:@selector(fontDescriptor)]) {
        return NO;
    }
    return (self.fontDescriptor.symbolicTraits & UIFontDescriptorTraitMonoSpace) > 0;
}

- (BOOL)isColorGlyphs
{
    if (![self respondsToSelector:@selector(fontDescriptor)]) {
        return NO;
    }
    return (CTFontGetSymbolicTraits((__bridge CTFontRef)self) & kCTFontTraitColorGlyphs) != 0;
}

- (CGFloat)fontWeight
{
    NSDictionary *traits = [self.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
    return [traits[UIFontWidthTrait] floatValue];
}

- (nullable UIFont *)fontWithBold
{
    if (![self respondsToSelector:@selector(fontDescriptor)]) {
        return self;
    }
    
    return [UIFont fontWithDescriptor:[self.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold] size:self.pointSize];
}

- (nullable UIFont *)fontWithItalic
{
    if (![self respondsToSelector:@selector(fontDescriptor)]) {
           return self;
       }
    return [UIFont fontWithDescriptor:[self.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic] size:self.pointSize];
}

- (nullable UIFont *)fontWithBoldItalic
{
    if (![self respondsToSelector:@selector(fontDescriptor)]) {
        return self;
    }
    
    return [UIFont fontWithDescriptor:[self.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic] size:self.pointSize];
}

+ (nullable UIFont *)fontWithCTFont:(CTFontRef)CTFont;
{
    if (!CTFont) return nil;
    CFStringRef name = CTFontCopyPostScriptName(CTFont);
    if (!name) return nil;
    CGFloat size = CTFontGetSize(CTFont);
    UIFont *font = [self fontWithName:(__bridge NSString *)(name) size:size];
    CFRelease(name);
    return font;
}

+ (UIFont *)fontWithCGFont:(CGFontRef)CGFont size:(CGFloat)size
{
    if (!CGFont) return nil;
    CFStringRef name = CGFontCopyPostScriptName(CGFont);
    if (!name) return nil;
    UIFont *font = [self fontWithName:(__bridge NSString *)(name) size:size];
    CFRelease(name);
    return font;
}

- (nullable CTFontRef)CTFontRef CF_RETURNS_RETAINED
{
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.fontName,  self.pointSize, NULL);
    return font;
}

- (nullable CGFontRef)CGFontRef CF_RETURNS_RETAINED
{
    CGFontRef font = CGFontCreateWithFontName((__bridge CFStringRef)self.fontName);
    return font;
}

//// 系统普通字体，字号，根据屏幕适配改版大小
//+ (UIFont *)fontScaleByDevice:(CGFloat)fontSize
//{
//    return [UIFont systemFontOfSize:kDeviceScaleFactor(fontSize)];
//}
//
//// 系统普通加粗字体，字号, 根据屏幕适配改版大小
//+ (UIFont *)boldFontScaleByDevice:(CGFloat)fontSize
//{
//    return [UIFont boldSystemFontOfSize:kDeviceScaleFactor(fontSize)];
//}
@end
